package com.stoken.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stoken.dao.LineUseDao;
import com.stoken.dao.MemberBaseInfoDao;
import com.stoken.dao.MemberRegisterInfoDao;
import com.stoken.domain.LineUse;
import com.stoken.domain.MemberBaseInfo;
import com.stoken.domain.MemberRegisterInfo;
import com.stoken.service.LineUseService;
import com.symbio.dao.hibernate.GenericDaoImpl;
import com.symbio.service.impl.AbstractServiceImpl;

@Service
public class LineUseServiceImpl extends AbstractServiceImpl<LineUse, Long> implements LineUseService{
    
	@Autowired
	private LineUseDao lineUseDao;
	
	@Autowired
	private MemberBaseInfoDao memberBaseInfoDao;
	
	@Autowired 
	private MemberRegisterInfoDao memberRegisterInfoDao;
	
	  
	@Override
	public GenericDaoImpl<LineUse, Long> getDao() {		
		return lineUseDao;
	}

	@Override
	public void init() {		
		List<MemberRegisterInfo> mRegisterInfo = memberRegisterInfoDao.getActivedMembers();
		for(MemberRegisterInfo mRInfo:mRegisterInfo){
			  MemberBaseInfo meBaseInfo = memberBaseInfoDao.find("baseUsername", mRInfo.getBaseUsername());
			  LineUse lineUse = new LineUse();
			  if(mRInfo.getRegisterGrade() == 1){
				  lineUse.setCanUseOther(2);
				  lineUse.setCanUseOws(0);
			  }else if(mRInfo.getRegisterGrade() == 2){
				  lineUse.setCanUseOther(8);
				  lineUse.setCanUseOws(0);
			  }else if(mRInfo.getRegisterGrade() == 5){
				  lineUse.setCanUseOther(30);
				  lineUse.setCanUseOws(0);
			  }else if(mRInfo.getRegisterGrade() == 10){
				  lineUse.setCanUseOther(100);
				  lineUse.setCanUseOws(0);
			  }else if(mRInfo.getRegisterGrade() == 30){
				  lineUse.setCanUseOther(100);
				  lineUse.setCanUseOws(0);
			  }
			  lineUse.setBaseUsername(meBaseInfo);
			  lineUseDao.save(lineUse);			 
		}		
		
	}

	@Override
	public LineUse getLineUseByBaseUserName(String baseUseName) {
		String hql1 = "from LineUse where baseUsername.baseUsername = ?";
		LineUse lineUse = lineUseDao.find(hql1 , new Object[]{baseUseName});
		return lineUse;
	}
	
	

}
